package test;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import cn.edu.nju.software.cdiy.bean.*;
import cn.edu.nju.software.cdiy.dao.*;
import cn.edu.nju.software.cdiy.dao.impl.*;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

public class CopyOftest {
	public static void main(String[] args) {
		tet();
	}
	public static void tet(){
		AbstractApplicationContext ctx = new FileSystemXmlApplicationContext(
				"D:\\finalworkspace2\\ComputerDIY\\WebRoot\\WEB-INF\\springConfig\\applicationContext.xml");
		OrderHardwareDao horderDao = (OrderHardwareDao) ctx.getBean("orderHardwareDao");
		horderDao.uorderToOrderHardware();
	}

	public static void testSave() {
		AbstractApplicationContext ctx = new FileSystemXmlApplicationContext(
				"F:\\J2EE\\workspace\\ComputerDIY\\WebRoot\\WEB-INF\\springConfig\\applicationContext.xml");
		HorderDao horderDao = (HorderDao) ctx.getBean("horderDao");
		SupplierDao supplierDao = (SupplierDao) ctx.getBean("supplierDao");
		ValueDao valueDao = (ValueDao) ctx.getBean("valueDao");
		SessionFactory factory = (SessionFactory) ctx.getBean("sessionFactory");

		/*
		 * private Long id; private Supplier supplier; private Date time;
		 * private Value state; private Set<OrderHardware> orderHardwares = new
		 * HashSet<OrderHardware>(); private String remark; private String rsv1;
		 * private String rsv2; private String rsv3;
		 */
		Horder h = new Horder();
		Supplier supplier = supplierDao.login("second", "second");
		h.setSupplier(supplier);
		h.setTime(new Date(System.currentTimeMillis()));
		h.setState(valueDao.getValue(BusinessConstant.HORDER_STATE1));
		h.setRemark("test");
		OrderHardware hardware = new OrderHardware();
		/*
		 * private Long id; private Long hardwareId; private String
		 * hardwareName; private BigDecimal price; private int num; private
		 * Value hardwareType; private String rsv1; private String rsv2; private
		 * String rsv3;
		 */
		hardware.setHardwareId((long) 1);
		hardware.setHardwareName("七彩虹iGame550Ti 烈焰战神U D5 1024M R50");
		hardware.setPrice(new BigDecimal(100));
		hardware.setNum(100);
		hardware.setHardwareType(valueDao
				.getValue(BusinessConstant.H_GRAPHICSA));
		Session s = factory.openSession();
		Transaction action = s.beginTransaction();
		s.save(h);
		System.out.println(h.getId());
		h.getOrderHardwares().add(hardware);
	//	hardware.setHorderId(h);

		s.save(hardware);
		System.out.println(hardware.getId());

		action.commit();
		s.close();

	}

	private static void testLoad() {
		AbstractApplicationContext ctx = new FileSystemXmlApplicationContext(
				"F:\\J2EE\\workspace\\ComputerDIY\\WebRoot\\WEB-INF\\springConfig\\applicationContext.xml");
		SessionFactory factory = (SessionFactory) ctx.getBean("sessionFactory");
		Session s = factory.openSession();
		Transaction action = s.beginTransaction();
		Horder h = (Horder) s.load(Horder.class, (long) 1151);
		System.out.println(h.getOrderHardwares().size());
	}

	private static void addUorder() {
		AbstractApplicationContext ctx = new FileSystemXmlApplicationContext(
				"D:\\finalworkspace2\\ComputerDIY\\WebRoot\\WEB-INF\\springConfig\\applicationContext.xml");
		SessionFactory factory = (SessionFactory) ctx.getBean("sessionFactory");
		SchemeDao schemeDao = (SchemeDao) ctx.getBean("schemeDao");
		UserDao userDao = (UserDao) ctx.getBean("userDao");
		ValueDao valueDao = (ValueDao) ctx.getBean("valueDao");
		Session s = factory.openSession();
		Transaction action = s.beginTransaction();
		/*
		 * private Long id;
		 * 
		 * private Scheme scheme; private int price; private User user; private
		 * Date time; private Value state; private String remark; private String
		 * deliveryAddress; private String rsv2; private String rsv3;
		 */
		Uorder u = new Uorder();
		Scheme scheme = schemeDao.getSchemeById((long) 1041);
		u.setScheme(scheme);
		u.setPrice(100);
		User user = userDao.getUserById((long) 1080);
		u.setUser(user);
		u.setTime(new Date(System.currentTimeMillis()));
		u.setState(valueDao.getValue(BusinessConstant.UORDER_STATE1));
		u.setRemark("test");
		u.setDeliveryAddress("test");
		s.save(u);
		System.out.println(u.getId());
		action.commit();
		s.close();

	}
	public static void testDateCompare()
	{
		AbstractApplicationContext ctx = new FileSystemXmlApplicationContext(
				"F:\\J2EE\\workspace\\ComputerDIY\\WebRoot\\WEB-INF\\springConfig\\applicationContext.xml");
		SessionFactory factory = (SessionFactory) ctx.getBean("sessionFactory");
		OrderHardwareDao dao=(OrderHardwareDao)ctx.getBean("orderHardwareDao");
		GregorianCalendar registerDate = new GregorianCalendar();
		GregorianCalendar endDate = new GregorianCalendar(
				registerDate.get(Calendar.YEAR),
				registerDate.get(Calendar.MONTH),
				registerDate.get(Calendar.DAY_OF_MONTH),
				registerDate.get(Calendar.HOUR_OF_DAY), 0);
		endDate.add(Calendar.HOUR_OF_DAY, 1);
		GregorianCalendar beginDate = new GregorianCalendar(
				registerDate.get(Calendar.YEAR),
				registerDate.get(Calendar.MONTH),
				registerDate.get(Calendar.DAY_OF_MONTH),
				registerDate.get(Calendar.HOUR_OF_DAY), 0);
		beginDate.add(Calendar.HOUR_OF_DAY, -12);
		dao.loadUorderByDate2(beginDate, endDate);
	}
}
